package com.hks360.car_treasure.listener;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.TextureMapView;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.hks360.car_treasure.R;
import com.hks360.car_treasure.common.BaiduMapConfig;
import com.hks360.car_treasure.common.Method;
import com.hks360.car_treasure.common.PrefKey;
import com.hks360.car_treasure.fragment.TrackFragment;
import com.hks360.car_treasure.util.LogUtil;
import com.hks360.car_treasure.util.MyUtil;
import com.hks360.library.util.CommonUtil;
import com.hks360.library.util.PrefUtil;

/**
 * Created by xiaote on 2016/3/16.
 */
public class TrakeLocationListener  implements BDLocationListener  {
    private TextureMapView mapView;
    private BaiduMap baiduMap;
    private boolean isFirstLoc;
    private LocationClient locClient;
    private Context context;
    private LatLng ll = null;
    private GeoCoder Search;
    public InfoWindow mInfoWindow;
    private String cph;

    public  TrakeLocationListener() {

    }

    public void init(GeoCoder mSearch,Context context,TextureMapView mapView,BaiduMap baiduMap,boolean isFirstLoc) {
        cph = Method.getCph();
        this.mapView = mapView;
        this.baiduMap = baiduMap;
        this.isFirstLoc = isFirstLoc;
        this.context = context;
        this.Search = mSearch;
    }

    public void startLocation(Context context) {
        mapView.showZoomControls(false);
        // 开启定位图层
        baiduMap.setMyLocationEnabled(true);
        // 定位初始化
        if (locClient != null) {
            locClient.stop();
        }
        isFirstLoc = true;
        locClient = new LocationClient(context);
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true);                                //打开gps
        option.setCoorType("bd09ll");                           //设置坐标类型为bd09ll
        option.setPriority(LocationClientOption.NetWorkFirst);  //设置网络优先
        option.setProdName("locSDKDemo2");                      //设置产品线名称
        option.setScanSpan(5000);                               //定时定位，每隔5秒钟定位一次。
        locClient.setLocOption(option);
        locClient.registerLocationListener(this);
        locClient.setLocOption(MyUtil.getLationClientOption());
        locClient.start();
    }

    @Override
    public void onReceiveLocation(BDLocation location) {
        if (location == null || mapView == null) {
            return;
        }
        LogUtil.d(location.getLatitude() + ","  +location.getLongitude());
        // map view 销毁后不在处理新接收的位置
        ll = new LatLng(location.getLatitude(),location.getLongitude());
        Search.reverseGeoCode(new ReverseGeoCodeOption()
                .location(ll));
        MyLocationData locData = new MyLocationData.Builder()
                .accuracy(5)
                // 此处设置开发者获取到的方向信息，顺时针0-360
                .direction(BaiduMapConfig.MAP_ROTATE_DEFAULT).latitude(location.getLatitude())
                .longitude(location.getLongitude()).build();
        baiduMap.setMyLocationData(locData);
        if (isFirstLoc) {
            isFirstLoc = false;
            ll = new LatLng(location.getLatitude(),
                    location.getLongitude());
            MapStatus.Builder builder = new MapStatus.Builder();
            builder.target(ll).zoom(BaiduMapConfig.ZOOM_DEFAULT);
            baiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
            locClient.stop();
            //第三处添加
            if(!CommonUtil.isEmptyStr(cph)) {
                TextView textView = new TextView(context);
                textView.setBackgroundResource(R.drawable.popup);
                textView.setText(Method.getCph());
                textView.setGravity(Gravity.CENTER);
                InfoWindow.OnInfoWindowClickListener listener = null;
                listener = new InfoWindow.OnInfoWindowClickListener() {
                    public void onInfoWindowClick() {
                        baiduMap.hideInfoWindow();
                    }
                };
                mInfoWindow = new InfoWindow(BitmapDescriptorFactory.fromView(textView), ll, -27, listener);
                baiduMap.showInfoWindow(mInfoWindow);
            }
        }
    }
    public void onReceivePoi(BDLocation poiLocation) {
    }
}
